package com.kehao.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.additional.query.impl.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kehao.annotation.AutoFill;
import com.kehao.dto.StuClassDto;
import com.kehao.enumeration.OperationType;
import com.kehao.mapper.StuClassMapper;
import com.kehao.pojo.StuClass;
import com.kehao.pojo.Student;
import com.kehao.service.StuClassService;
import com.kehao.vo.StuClassRec;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Optional;
import java.util.function.Consumer;
import java.util.stream.Collectors;

@Service
public class StuClassServiceImpl extends ServiceImpl<StuClassMapper, StuClass> implements StuClassService {
    @Override
    public List<StuClassRec> getAll() {

        //查询并封装返回值对象
        return list()
                .stream()
                .map(s -> new StuClassRec(s.getId(),
                        s.getUserName(),
                        s.getStudentId(),
                        s.getChineseScore(),
                        s.getMathScore(),
                        s.getEnglishScore(),
                        s.getStatus(),
                        s.getEnrollmentDate(),
                        s.getUpdateTime())).collect(Collectors.toList());
    }

    @AutoFill(OperationType.INSERT)
    @Override
    public boolean add(StuClassDto stuClassDto) {

        StuClass stuClass = new StuClass();
        //属性拷贝
        BeanUtil.copyProperties(stuClassDto, stuClass);

        return save(stuClass);

    }

    @Override
    public boolean del(String student_id) {

        //查找对应的学生id并删除
        return remove(new LambdaQueryWrapper<StuClass>()
                .eq(student_id != null, StuClass::getStudentId, student_id));


    }

    @AutoFill(OperationType.UPDATE)
    @Override
    public boolean updateClass(StuClassDto stuClassDto) {

        //根据id修改
        return lambdaUpdate()
                .set(!stuClassDto.getUserName().isBlank(), StuClass::getUserName, stuClassDto.getUserName())
                .set(!stuClassDto.getPassword().isBlank(), StuClass::getPassword, stuClassDto.getPassword())
                .set(StuClass::getMathScore, stuClassDto.getMathScore())
                .set(StuClass::getChineseScore, stuClassDto.getChineseScore())
                .set(StuClass::getEnglishScore, stuClassDto.getEnglishScore())
                .set(!stuClassDto.getStatus().isBlank(), StuClass::getStatus, stuClassDto.getStatus())
                .eq(!stuClassDto.getStudentId().isBlank(), StuClass::getStudentId, stuClassDto.getStudentId())
                .update();
    }

    @Override
    public StuClassRec getByOne(String id) {

        //optional
        //如果对象为null， 则返回自定义的null。不为空则返回对象
        StuClass result = Optional.ofNullable(lambdaQuery()
                        .eq(StuClass::getStudentId, id).one())
                .orElseGet(() -> new StuClass(null, null, null, null,
                        null, null, null, null, null, null));
        //封装返回值并返回
        return new StuClassRec(result.getId(),
                result.getUserName(),
                result.getStudentId(),
                result.getChineseScore(),
                result.getMathScore(),
                result.getEnglishScore(),
                result.getStatus(),
                result.getEnrollmentDate(),
                result.getUpdateTime());
    }
}
